const db = require('../db/index');

// 发布消息 where 前面要有空格
exports.publishMessage = (req, res) => {
  let {
    message_title,
    message_category,
    message_publish_department,
    message_publish_name,
    message_receipt_object,
    message_content,
    message_level,
  } = req.body;
  let message_publish_time = new Date();
  let message_click_number = 0;
  let message_status = 0; // 0表示未读，1表示已读
  let sql = 'insert into message set ?';
  db.query(sql, {
    message_title,
    message_category,
    message_publish_department,
    message_publish_name,
    message_receipt_object,
    message_content,
    message_level,
    message_click_number,
    message_publish_time,
    message_status
  }, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc('发布消息失败');
    res.send({
      status: 0,
      message: '发布消息成功'
    })
  })
}

// 获取公司消息
exports.getCompanyMessage = (req, res) => {
  let sql = 'select * from message where message_category="公司公告" and message_status="0"';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      results
    })
  })
}

// 获取系统消息
exports.getSystemMessage = (req, res) => {
  let sql = 'select * from message where message_category="系统消息" and message_status="0"';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      results
    })
  })
}

// 编辑公司公告消息
exports.editCompanyMessage = (req, res) => {
  let {
    id,
    message_title,
    message_receipt_object,
    message_publish_name,
    message_content,
    message_level,
  } = req.body;
  let message_update_time = new Date();
  let sql = 'update message set message_title=?,message_receipt_object=?,message_publish_name=?,message_content=?,message_level=?,message_update_time=? where id=?';
  db.query(sql, [
    message_title,
    message_receipt_object,
    message_publish_name,
    message_content,
    message_level,
    message_update_time,
    id
  ], (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc('更新消息失败');
    res.send({
      status: 0,
      message: '更新消息成功'
    })
  })
}

// 通过部门获取消息
exports.getMessageByDepartment = (req, res) => {
  let sql = 'select * from message where message_publish_department=? and message_status="0"';
  db.query(sql, req.query.message_publish_department, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      results
    })
  })
}

// 通过等级获取消息
exports.getMessageByLevel = (req, res) => {
  let sql = 'select * from message where message_level=? and message_status="0"';
  db.query(sql, req.query.message_level, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      results
    })
  })
}

// 通过id获取消息
exports.getMessageById = (req, res) => {
  let sql = 'select * from message where id=?';
  db.query(sql, req.query.id, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      results
    })
  })
}

// 跟新点击率
exports.updateClickNumber = (req, res) => {
  let {
    id,
    message_click_number
  } = req.body;
  // *1 保证字段为数字类型
  message_click_number = message_click_number * 1 + 1;
  let sql = 'update message set message_click_number=? where id=?';
  db.query(sql, [message_click_number, id], (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc('更新点击率失败');
    res.send({
      status: 0,
      message: '更新点击率成功'
    })
  })
}

// 初次删除消息
exports.deleteFirstMessage = (req, res) => {
  let message_status = 1;
  let sql = 'update message set message_status=? where id=?';
  db.query(sql, [message_status, req.body.id], (err, results) => {

    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc('删除消息失败');
    res.send({
      status: 0,
      message: '删除消息成功'
    })
  })
}

// 获取回收站消息列表
exports.getReviceMessageList = (req, res) => {
  let sql = 'select * from message where message_status=1';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      results
    })
  })
}

// 还原消息
exports.reciveMessage = (req, res) => {
  let message_status = 0
  let sql = 'update message set message_status=? where id=?';
  db.query(sql, [message_status, req.body.id], (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc('还原消息失败');
    res.send({
      status: 0,
      message: '还原消息成功'
    })
  })
}

// 删除信息
exports.deleteMessage = (req, res) => {
  let sql = 'delete from message where id=?';
  db.query(sql, req.body.id, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc('删除消息失败');
    res.send({
      status: 0,
      message: '删除消息成功'
    })
  })
}